package com.citrix.gs.test.perforce;

import java.net.URISyntaxException;
import java.util.List;

import org.apache.commons.io.IOUtils;

import com.perforce.p4java.core.file.FileSpecBuilder;
import com.perforce.p4java.core.file.FileSpecOpStatus;
import com.perforce.p4java.core.file.IFileSpec;
import com.perforce.p4java.exception.P4JavaException;
import com.perforce.p4java.option.server.GetDepotFilesOptions;
import com.perforce.p4java.option.server.GetDirectoriesOptions;
import com.perforce.p4java.server.IOptionsServer;
import com.perforce.p4java.server.callback.IProgressCallback;

public class ListFilesDemo extends P4JavaDemo {

    public static void main(String[] args) throws URISyntaxException, P4JavaException {
        
        serverUri = "p4java://NKGP401.eng.citrite.net:1111";
        userName = "yuzho";
        password = "Szqy$1135";
        
        IOptionsServer server = getOptionServer(null, null);
        //TODO: check how the callback is executed
        server.registerProgressCallback(new DemoProgressCallback());
        
        server.setUserName(userName);
        server.login(password);
        
        System.out.println("Depot files on Perforce server at URI '" + serverUri + "'");
        
        List<IFileSpec> fileList = server.getDepotFiles(
                FileSpecBuilder.makeFileSpecList("//localization/LocEngineering/LocEngg-Tools/BeaconToolset/ResourceDetector/..."), new GetDepotFilesOptions());
        
       
        
        if(fileList != null) {
            fileList.forEach(f -> {
                if(f != null) {
                    if(f.getOpStatus() == FileSpecOpStatus.VALID) {
                        System.out.println(formatFileSpec(f));
                        try {
                            //String theString = IOUtils.toString(f.getContents(true), "UTF-8");
                            //System.out.println(theString);
                            System.out.println(f.getDate());
                            
                            
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else {
                        System.err.println(f.getStatusMessage());
                    }
                }
            });
        }
        
        List<IFileSpec> directoryList = server.getDirectories(
                FileSpecBuilder.makeFileSpecList("//localization/LocEngineering/LocEngg-Tools/BeaconToolset/ResourceDetector/..."), new GetDirectoriesOptions());
        
        if(directoryList != null) {
            directoryList.forEach(f -> {
                if(f != null) {
                    if(f.getOpStatus() == FileSpecOpStatus.VALID) {
                        System.out.println(formatFileSpec(f));
                        try {
                            //String theString = IOUtils.toString(f.getContents(true), "UTF-8");
                            //System.out.println(theString);
                            System.out.println(f.getDate());
                            
                            
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else {
                        System.err.println(f.getStatusMessage());
                    }
                }
            });
        }
    }
    
    protected static String formatFileSpec(IFileSpec fileSpec) {
        return fileSpec.getDepotPathString();
    }
    
    protected static class DemoProgressCallback implements IProgressCallback {
        
        public void start(int key) {
            System.out.println("Starting command " + key);
        }
 
        public void stop(int key) {
            System.out.println("Stopping command " + key);
        }
 
        public boolean tick(int key, String tickMarker) {
            if (tickMarker != null) {
                System.out.println(key + " - " + tickMarker);
            }
            return true;
        }
    }
}
